package com.xiaohao.communication.mobileUtil;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-07-31 21:04
 * @package com.inspur_video.inspur_video.mobileUtil
 * @description
 */
public class SendMsg {

    //发送验证码的请求路径URL
    private static final String
            SERVER_URL = "https://api.netease.im/sms/sendcode.action";

    private static final String
            SERVER_URL_VERIFY="https://api.netease.im/sms/verifycode.action";
    private static final String
            APP_KEY = "508edf77910e71d6cfe7c11896974cfd";
    private static final String APP_SECRET = "ad5c5bdb0403";

    //随机数
    private static final String NONCE = "123456";

    //短信模板ID
    private static final String TEMPLATEID = "9784239";
    //验证码长度，范围4～10，默认为4
    private static final String CODELEN = "4";


    public static String sendMsg(String mobie) throws Exception {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(SERVER_URL);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

        // 设置请求的header
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");


        List<NameValuePair> nvps = new ArrayList<NameValuePair>();


        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile", mobie));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));


        HttpResponse response = httpClient.execute(httpPost);
        /*
         * 1.打印执行结果，打印结果一般会200、315、403、404、413、414、500
         * 2.具体的code有问题的可以参考官网的Code状态表
         */

        return EntityUtils.toString(response.getEntity(), "utf-8");

    }



    public static String VerifyCode(String mobie,String code) throws Exception {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(SERVER_URL_VERIFY);
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

        // 设置请求的header
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");


        List<NameValuePair> nvps = new ArrayList<NameValuePair>();


        nvps.add(new BasicNameValuePair("mobile", mobie));
        nvps.add(new BasicNameValuePair("classifications", code));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));


        HttpResponse response = httpClient.execute(httpPost);
        /*
         * 1.打印执行结果，打印结果一般会200、315、403、404、413、414、500
         * 2.具体的code有问题的可以参考官网的Code状态表
         */

        return EntityUtils.toString(response.getEntity(), "utf-8");

    }


}
